package com.luntan.action;

import java.util.Date;

import com.luntan.domain.MainTie;
import com.luntan.domain.TUser;
import com.luntan.service.HuiTieService;
import com.luntan.service.MainTieService;
import com.luntan.util.web.Page;

/**
 * 发帖控制层
 * 
 * @author mashiqiong
 *
 */
public class MainTieAction extends BaseAction {
	private Page<MainTie> page = new Page<MainTie>();
	private MainTieService mainTieService;
	private HuiTieService huiTieService;
	private MainTie mainTie=new MainTie(new TUser());

	/**
	 * 添加页面
	 *
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		return INPUT;
	}
	
	/**
	 * 列表
	 *
	 * @return
	 * @throws Exception
	 */
	public String addSave() throws Exception {
		mainTie.setMdate(new Date());
		boolean flag=this.mainTieService.addSave(mainTie);
		if(flag){
			return this.list();
		}else{
			addFieldError("all", "保存失败");
			return INPUT;
		}
	}
	
	/**
	 * 验证添加的项是否符合要求
	 */
	public void validateAddSave() {
		common();
	}

	private void common() {
		if(this.mainTie.getMtitle()==null||"".equals(mainTie.getMtitle())){
			addFieldError("mtitle", "帖子标题不能为空");
		}
		
		if(this.mainTie.getMcontent()==null||"".equals(mainTie.getMcontent())){
			addFieldError("mcontent", "帖子内容不能为空");
		}
	}
	
	
	/**
	 * 列表
	 *
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		page = this.mainTieService.getList(page.getCurrentPage(),page.getLimit(),mainTie);

		return "list";
	}

	
	/**
	 * 修改
	 *
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		this.mainTie=this.mainTieService.getEntity(MainTie.class, this.mainTie.getMid());
		return "update";
	}
	
	/**
	 * 保存修改
	 *
	 * @return
	 * @throws Exception
	 */
	public String updateSave() throws Exception {
		boolean flag=this.mainTieService.updateSave(mainTie);
		if(flag){
			return this.list();
		}else{
			addFieldError("all", "保存失败");
			return "update";
		}
	}
	
	/**
	 * 验证修改的项是否符合要求
	 */
	public void validateUpdateSave() {
		common();
	}
	
	/**
	 * 查看帖子
	 *
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		this.mainTie=this.mainTieService.getEntity(MainTie.class, this.mainTie.getMid());
		return "view";
	}
	
	/**
	 * 删除帖子
	 *
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		boolean flag=this.mainTieService.delete(this.mainTie.getMid());
		if(!flag){
			addActionError("删除帖子失败");
		}else{
			addActionMessage("删除帖子成功");
		}
		
		return "listMainTie";	
	}
	
	public Page<MainTie> getPage() {
		return page;
	}

	public void setPage(Page<MainTie> page) {
		this.page = page;
	}

	public MainTie getMainTie() {
		return mainTie;
	}

	public void setMainTie(MainTie mainTie) {
		this.mainTie = mainTie;
	}

	public void setMainTieService(MainTieService mainTieService) {
		this.mainTieService = mainTieService;
	}

	public void sethuiTieService(HuiTieService huiTieService) {
		this.huiTieService = huiTieService;
	}
}
